/*
 * This class is a multiton class,you can use addScoccer() to get a Soccer set.
 * 
 * public :
 * 		public static Set<Scoccer> addScoccer(String scoccerName);
 * 		public String getName();
 * 
 * private:
 * 		private static Set<Scoccer> scoccerSet = new HashSet<Scoccer>();
 * 		private String name;
 * 		private Soccer(String scoccerName);
 */
package singleton;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Soccer
{
	// private set to make sure other object could't get this objects
	private static Set<Soccer> soccerSet = new HashSet<Soccer>();
	private String name;

	// private constructor to make sure other object could't new any object
	private Soccer(String scoccerName)
	{
		this.name = scoccerName;
	}

	// public static for other object
	public static Set<Soccer> addScoccer(String soccerName)
	{
		// this block make sure soccer's name is different
		// if the method get a name that is repeated,it will return the set
		// not add the same-name soccer into set
		Iterator<Soccer> it = soccerSet.iterator();
		while (it.hasNext())
		{
			if (it.next().getName().equals(soccerName))
				return soccerSet;
		}

		// this block make sure soccer set is less than 10
		if (soccerSet.size() < 10)
		{
			Soccer sco = new Soccer(soccerName);
			soccerSet.add(sco);
		}
		return soccerSet;
	}
	public static Set<Soccer> getSet()
	{
		return Soccer.soccerSet;
	}
	public String getName()
	{
		return this.name;
	}
}
